package com.dayang.nm.ugcaudit.action;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public abstract class BaseAction extends Action {
	public static Logger logger = Logger.getLogger(BaseAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);

		Method method = null;
		logger.debug(" ");
		logger.debug("------------------------------------request begin----------------------------------------");
		String act = request.getParameter("act");
		Class thisCalss = this.getClass();
		logger.debug("resource : "+thisCalss.getName() +" ; act : " + act);

		if (act.length() > 0) {
			try {
				Class[] classparam = new Class[4];
				classparam[0] = ActionMapping.class;
				classparam[1] = ActionForm.class;
				classparam[2] = HttpServletRequest.class;
				classparam[3] = HttpServletResponse.class;
				method = this.getClass().getMethod(act, classparam);
				Object[] classparamvalue = new Object[4];
				classparamvalue[0] = mapping;
				classparamvalue[1] = form;
				classparamvalue[2] = request;
				classparamvalue[3] = response;
				
				String rtp = method.getReturnType().getName();
				if(rtp.equals("java.lang.String")){
					String rtn = (String) method.invoke(this, classparamvalue);
					logger.debug("response Str : " + rtn);
					if(rtn!=null){
				    	response.setContentType("text/html; charset=UTF-8");
						PrintWriter writer = response.getWriter();
						writer.write(rtn);
						writer.close();
					}
					
				}else {
					byte data[] = null;
					data = (byte []) method.invoke(this, classparamvalue);
					response.setContentType("image/jpeg; charset=UTF-8");
		            OutputStream toClient = response.getOutputStream();
		            toClient.write(data);
		            toClient.close();   
				}
				return null;
			} catch (Exception e) {
				logger.error("error", e);
			}
		}

		return null;
	}

	abstract public Object read(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception;

	abstract public String update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception;

	abstract public String create(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception;

	abstract public String delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception;
	
	public Object getObjectFromRequest(HttpServletRequest request,String objectName,Class objectClass){

		String objectStr = request.getParameter(objectName);
		JSONObject objectJson = JSONObject.fromObject(objectStr);
		Object object = JSONObject.toBean(objectJson, objectClass);//BasicInfoItem.class
		
		return object;
		
	}

}
